/**
 * By wangmingxue
 * Created by wangmingxue on 16/05/01.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'fineuploader',
    'calendar'
], function ($, common, ready, Collection, template, Model, dialog, fineuploader, calendar) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'data/order_back/' + created_sn,
                condition: {limit: 10, offset: 0},
                model: {
                    tag_name: 'tr',
                    url: {},
                    event: {
                        '.btn-add_user_tag': 'add_user_tag'
                    },
                    add_user_tag: function (e) {
                        var Tthis = this;
                        //console.log(Tthis);
                        //var goods_id = Tthis.data.goods_id;
                        var order_uid = Tthis.data.order_uid;
                        var created_at = Tthis.data.created_at;
                        var trace_data_id = Tthis.data.trace_data_id;
                        $.ajax({
                            url: _global.url.api + 'data/get_order_back',
                            data: {
                                //goods_id: goods_id,
                                order_uid: order_uid,
                                created_at: created_at,
                                trace_data_id: trace_data_id
                            },
                            type: 'get',
                            async: false,
                            success: function (res) {
                                dialog({
                                    title: '商品详情页浏览日志',
                                    modal: true,
                                    content: template('add-user-tag')
                                }).show();
                                if (res.code == 0) {
                                    var str = '';
                                    for (var i in res.data) {
                                        var temp = res.data[i];
                                        if (!!temp.user_id) {
                                            str += '<tr><td>' + i + '</td>' +
                                            '<td>' + temp.time_timestamp + '</td>' +
                                            '<td>' + temp.client_position + '</td>' +
                                            '<td>' + temp.pos_instr + '</td>' +
                                            '<td>' + temp.client_event + '</td>' +
                                            '<td>' + temp.event_instr + '</td>' +
                                            '<td>' + temp.client_expos + '</td>' +
                                            '<td>' + temp.url + '<br />' + temp.referer_url + '<br />' + temp.client_data + '</td>' +
                                            '<td>' + temp.card_id + '</td>' +
                                            '<td>' + temp.activity_id + '</td>' +
                                            '<td>' + temp.note_id + '</td>' +
                                            '<td>' + temp.site_id + '</td>' +
                                            '<td>' + temp.goods_id + '</td>' +
                                            '</tr>';
                                        }
                                    }
                                    $("#add-data-list").html(str);
                                }
                            }
                        });
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            var _this = this;
            //日期
            $('.form_datetime').calendar();
            //按照日期和标签查询
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.order_sn = $('.order_sn').val();
                _this.collection.reload();
            });
        }
    };
    new index();
});